package cn.zl.biz.controller.router;

import cn.zl.biz.entity.SysArticle;
import cn.zl.biz.entity.SysCategory;
import cn.zl.biz.entity.SysTag;
import cn.zl.biz.service.ArticleService;
import cn.zl.biz.service.CategoryService;
import cn.zl.biz.service.TagService;
import cn.zl.common.controller.BaseController;
import cn.zl.common.utils.R;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.annotation.Resource;
import java.util.List;

/**
 * 博客前台页面路由
 *
 * @author zl
 * @date 2023/9/12
 */
@Controller
public class RouterController extends BaseController {
    @Resource
    private TagService tagService;

    @Resource
    private CategoryService categoryService;

    @Resource
    private ArticleService articleService;

    @GetMapping("/")//显示博客的首页
    public String index() {
        return "index/index";
    }

    //根据文章id查询文章信息
    @GetMapping("/p/{id}")
    public String p(@PathVariable Long id, Model model) {
        SysArticle sysArticle = articleService.findById(id);
        if (sysArticle != null) {
            model.addAttribute("p", sysArticle);
        } else {
            return "error/404";
        }
        return "p/index";
    }

    //显示关于页面
    @GetMapping("/about")
    public String about() {
        return "about/index";
    }

    //显示友情链接
    @GetMapping("/links")
    public String links() {
        return "link/index";
    }


    //获取标签列表页
    @GetMapping("/tag/getAllTag")
    public String getAllTag(Model model){
        R<List<SysTag>> result = new R<>(tagService.findAllTags());
        if (result.getCode() == 200 && result.getData() != null) {
            // 将数据存储到Model中
            model.addAttribute("data", result.getData());
        } else {
            // 处理无数据的情况，可以设置一个错误消息
            model.addAttribute("error", "暂无数据，请稍后重试。");
        }
        return "index/tag";
    }

    //根据标签ID查询指定的文章列表页
    @GetMapping("/tag/article/{tagId}")
    public String getAllArticleByTagId(@PathVariable Integer tagId,Model model){
        R<List<SysArticle>> articleList = new R<>(tagService.findAllArticle(tagId));
        if (articleList.getCode() == 200 && articleList.getData() != null) {
            // 将数据存储到Model中
            model.addAttribute("data", articleList.getData());
        } else {
            // 处理无数据的情况，可以设置一个错误消息
            model.addAttribute("error", "暂无数据，请稍后重试。");
        }
        return "index/article";
    }


    //获取分类列表
    @GetMapping("/category/getAllCategory")
    public String getAllCategory(Model model){
        R<List<SysCategory>> result = new R<>(categoryService.findAllCategorys());
        if (result.getCode() == 200 && result.getData() != null) {
            // 将数据存储到Model中
            model.addAttribute("data", result.getData());
        } else {
            // 处理无数据的情况，可以设置一个错误消息
            model.addAttribute("error", "暂无数据，请稍后重试。");
        }
        return "index/category";
    }


    //根据分类ID查询指定的文章列表页
    @GetMapping("/category/article/{categoryId}")
    public String getAllArticleByCategoryId(@PathVariable Integer categoryId,Model model){
        R<List<SysArticle>> articleList = new R<>(categoryService.findAllArticle(categoryId));
        if (articleList.getCode() == 200 && articleList.getData() != null) {
            // 将数据存储到Model中
            model.addAttribute("data", articleList.getData());
        } else {
            // 处理无数据的情况，可以设置一个错误消息
            model.addAttribute("error", "暂无数据，请稍后重试。");
        }
        return "index/article";
    }

}
